import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport._
import com.typesafe.sbt.packager.docker._

ThisBuild / version := "0.1.0-SNAPSHOT"

lazy val org = "3.4.1"
lazy val scala3Version = "3.4.1"

lazy val root = (project in file("."))
  .settings(
    name := "tyrian-http4s"
  )

// 前后端公共共用模块
lazy val common = (crossProject(JSPlatform, JVMPlatform) in file("common"))
  .settings(
    name         := "common",
    scalaVersion := scala3Version,
    organization := org
  )
  .jvmSettings(
    // add here if necessary
  )
  .jsSettings(
    // Add JS-specific settings here
  )


// 前端模块
lazy val circeVersion  = "0.14.9"

lazy val tyrianVersion = "0.11.0"
lazy val fs2DomVersion = "0.1.0"
lazy val laikaVersion  = "0.19.0"

lazy val app = (project in file("app"))
  .enablePlugins(ScalaJSPlugin)
  .settings(
    name         := "app",
    scalaVersion := scala3Version,
    organization := org,
    libraryDependencies ++= Seq(
      "io.indigoengine" %%% "tyrian-io"     % tyrianVersion,
      "com.armanbilge"  %%% "fs2-dom"       % fs2DomVersion,
      "org.planet42"    %%% "laika-core"    % laikaVersion,
      "io.circe"        %%% "circe-core"    % circeVersion,
      "io.circe"        %%% "circe-parser"  % circeVersion,
      "io.circe"        %%% "circe-generic" % circeVersion,
      "org.http4s"      %%% "http4s-dom"    % "0.2.9",
    ),
    scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) },
    semanticdbEnabled := true,
    autoAPIMappings   := true,
  ).dependsOn(common.js)


// 后端模块
lazy val http4sVersion              = "0.23.27"
lazy val doobieVersion              = "1.0.0-RC5"
lazy val pureConfigVersion          = "0.17.1"
lazy val log4catsVersion            = "2.4.0"
lazy val tsecVersion                = "0.4.0"
lazy val scalaTestVersion           = "3.2.12"
lazy val scalaTestCatsEffectVersion = "1.4.0"
lazy val testContainerVersion       = "1.17.3"
lazy val logbackVersion             = "1.4.0"
lazy val slf4jVersion               = "2.0.0"
lazy val javaMailVersion            = "1.6.2"
lazy val tapirVersion               = "1.9.6"
lazy val JwtScalaVersion            = "9.4.4"

lazy val server = (project in file("server"))
  .settings(
    name         := "server",
    scalaVersion := scala3Version,
    organization := org,
    libraryDependencies ++= Seq(
      // http4s
      "org.http4s"            %% "http4s-dsl"                    % http4sVersion,
      "org.http4s"            %% "http4s-ember-server"           % http4sVersion,
      "org.http4s"            %% "http4s-ember-client"           % http4sVersion,
      "org.http4s"            %% "http4s-circe"                  % http4sVersion,

      // 接口设计和swagger
      "com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % tapirVersion,
      "com.softwaremill.sttp.tapir" %% "tapir-prometheus-metrics" % tapirVersion,
      "com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % tapirVersion,
      "com.softwaremill.sttp.tapir" %% "tapir-json-circe" % tapirVersion,
      "ch.qos.logback"              %  "logback-classic" % "1.4.11",


      // json操作
      "io.circe"              %% "circe-generic"                 % circeVersion,
      "io.circe"              %% "circe-fs2"                     % "0.14.1",

      // 连接数据库
      "org.tpolecat"          %% "doobie-core"                   % doobieVersion,
      "org.tpolecat"          %% "doobie-hikari"                 % doobieVersion,
      "org.tpolecat"          %% "doobie-postgres"               % doobieVersion,

      // 读取配置
      "com.github.pureconfig" %% "pureconfig-core"               % pureConfigVersion,

      // token验证
      "com.github.jwt-scala"    %% "jwt-circe"                   % JwtScalaVersion,

      // 打印日志
      "org.typelevel"         %% "log4cats-slf4j"                % log4catsVersion,
      "org.slf4j"              % "slf4j-simple"                  % slf4jVersion,
      "org.typelevel"         %% "log4cats-noop"                 % log4catsVersion            % Test,

      // 写test 用到的:根据需要添加
      "org.scalatest"         %% "scalatest"                     % scalaTestVersion           % Test,
      "org.typelevel"         %% "cats-effect-testing-scalatest" % scalaTestCatsEffectVersion % Test,
      "org.tpolecat"          %% "doobie-scalatest"              % doobieVersion              % Test,

    ),
    Compile / mainClass := Some("com.fullstack.server.Server")
  ).dependsOn(common.jvm)



// 打包成docker镜像
lazy val stagingBuild = (project in (file("build/staging")))
  .enablePlugins(JavaAppPackaging, DockerPlugin)
  .settings(
    name            := "zybkltw-staging",
    scalaVersion    := scala3Version,
    organization    := org,
    dockerBaseImage := "m.daocloud.io/docker.io/library/openjdk:11-jre-slim-buster",
    dockerExposedPorts := Seq(4046),
    dockerExposedVolumes := Seq("/opt/docker/logs"),
    dockerEnvVars := Map("APP_ENV" -> "prod", "OSS_ACCESS_KEY_ID" -> "LTAI5tP7axM3b9Yce1ZgkA72", "OSS_ACCESS_KEY_SECRET" -> "AVVqkx8ZK9BKeRy2jqzYMEQkSi9JtF"),
    dockerCommands += Cmd("USER", "root"),
    Compile / mainClass         := Some("com.fullstack.server.Server"),
    Compile / resourceDirectory := ((server / Compile / resourceDirectory).value / "staging")
  )
  .dependsOn(server)